<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace api\v1\model;

use think\Model;

class CmfRoleModel extends Model
{
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'role';

    /**
     * 获得所有角色
     * @param       $filter
     * @param       $field
     * @param       $pageNumber
     * @param       $pageSize
     * @return \think\Paginator
     */
    public function getAll($filter, $field = "*", $pageNumber = 1, $pageSize = 20){

        /*
        $where = function (Query $query) use ($filter) {
            $keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
            if (!empty($keyword)) {
                $query->whereOr('zzjg_mc', 'like', "%$keyword%");
            }

            $zzjg_id = empty($filter['zzjg_id']) ? '' : $filter['zzjg_id'];
            if (!empty($zzjg_id)) {
                $query->whereOr('id', '=', $zzjg_id);
            }
        };*/

        $where = [['create_time','>=',0],['id','>',2]];

        $field = "*";
        $order = ['list_order' => 'asc'];

        $total = $this
            ->where($where)
            ->count();

        $res = $this->field($field)
            ->where($where)
            ->order($order)
            ->limit($pageSize)
            ->page($pageNumber)
            ->select();

        /** ->fetchSql(true) ，查询参数：?keyword=湖&zzjg_id=3，生成sql：
         * "SELECT * FROM `xmgl_zzjg` WHERE  
         *  `create_time` >= 0  AND `delete_time` = 0  AND
         * (  `zzjg_mc` LIKE '%湖%'  OR `id` = 3 ) 
         * ORDER BY `list_order` DESC LIMIT 0,20"
         */

        $d = [
            'pageSize'          => $pageSize,
            'pageNumber'        => $pageNumber,
            "totalPage"         => ceil($total / $pageSize),
            'total'             => $total,
            'rows'              => $res,
            //'where' => $sqlstr,
        ];
        return $d;
    }
}
